/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.pro.web;

import java.io.File;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.commons.CommonsMultipartFile;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.common.utils.FileUtils;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.common.utils.UploadUtils;
import com.thinkgem.jeesite.modules.pro.entity.Document;
import com.thinkgem.jeesite.modules.pro.service.DocumentService;

/**
 * 文档管理Controller
 * @author fanqinglong
 * @version 2017-05-03
 */
@Controller
@RequestMapping(value = "${adminPath}/pro/document")
public class DocumentController extends BaseController {

	@Autowired
	private DocumentService documentService;
	
	@ModelAttribute
	public Document get(@RequestParam(required=false) String id) {
		Document entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = documentService.get(id);
		}
		if (entity == null){
			entity = new Document();
		}
		return entity;
	}
	
	@RequiresPermissions("pro:document:view")
	@RequestMapping(value = {"list", ""})
	public String list(Document document, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<Document> page = documentService.findPage(new Page<Document>(request, response), document); 
		model.addAttribute("page", page);
		model.addAttribute("document", document);
		return "modules/pro/documentList";
	}

	@RequiresPermissions("pro:document:view")
	@RequestMapping(value = "form")
	public String form(Document document, Model model) {
		model.addAttribute("document", document);
		return "modules/pro/documentForm";
	}
	@RequiresPermissions("pro:document:view")
	@RequestMapping(value = "down")
	@ResponseBody
	public String down(Document document, Model model,HttpServletResponse response,HttpServletRequest request) {
		File file = new File(document.getSrc());
		model.addAttribute("document", document);
		return FileUtils.downFile(file, request, response);
	}

	@RequiresPermissions("pro:document:edit")
	@RequestMapping(value = "save")
	public String save(@RequestParam("file") CommonsMultipartFile file,Document document, Model model, RedirectAttributes redirectAttributes,HttpServletRequest request,HttpServletResponse response) {
		if (!beanValidator(model, document)){
			return form(document, model);
		}
		response.setContentType("text/html;charset=UTF-8");
		String[] info = new UploadUtils().uploadFile(request,file);
		if ("true".equals(info[0])) {
			document.setName(info[4].substring(info[4].lastIndexOf("/")+1));
		}
		documentService.save(document);
		
		addMessage(redirectAttributes, "保存文档成功");
		return "redirect:"+Global.getAdminPath()+"/pro/document/?repage&folder.id="+document.getFolder().getId();
	}
	
	@RequiresPermissions("pro:document:edit")
	@RequestMapping(value = "delete")
	public String delete(Document document, RedirectAttributes redirectAttributes) {
		documentService.delete(document);
		addMessage(redirectAttributes, "删除文档成功");
		return "redirect:"+Global.getAdminPath()+"/pro/document/?repage";
	}

}